package net.zio.kamenriderzioaddon.ridersystem.registry;

import net.minecraft.ChatFormatting;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.zio.kamenriderzioaddon.enums.HenshinState;
import net.zio.kamenriderzioaddon.enums.SoundType;
import net.zio.kamenriderzioaddon.ridersystem.data.PlayerDataManager;
import net.zio.kamenriderzioaddon.ridersystem.util.HenshinUtils;

import java.util.Collections;
import java.util.Map;
import java.util.function.Predicate;

public class BaseRiderConfig {
    // 核心配置属性
    protected final String riderName;
    protected final Item[] armorSet;
    protected final Predicate<Player> transformCondition;
    protected final Item mainHenshinItem;
    protected final Item offhandHenshinItem;

    // 常量定义
    protected static final EquipmentSlot[] SLOTS = HenshinUtils.ARMOR_SLOTS;
    protected static final EquipmentSlot MAINHAND_SLOT = EquipmentSlot.MAINHAND;
    protected static final EquipmentSlot OFFHAND_SLOT = EquipmentSlot.OFFHAND;

    /**
     * 基础配置类构造函数
     *
     * @param riderName 骑士名称
     * @param armorSet 基础形态装备数组
     * @param transformCondition 变身条件函数
     * @param mainHenshinItem    主手变身道具（可为空）
     * @param offhandHenshinItem  副手变身道具（可为空）
     */
    public BaseRiderConfig(String riderName, Item[] armorSet,
                           Predicate<Player> transformCondition, Item mainHenshinItem,Item offhandHenshinItem) {
        this.riderName = riderName;
        this.armorSet = armorSet;
        this.transformCondition = transformCondition;
        this.mainHenshinItem = mainHenshinItem;
        this.offhandHenshinItem = offhandHenshinItem;
    }


    // 检查是否符合变身条件
    public boolean canTransform(Player player) {
        return transformCondition.test(player);
    }

    /* -------------状态操纵-------------- */
    // 获取当前状态
    public HenshinState getCurrentState(Player player) {
        return PlayerDataManager.get(player).getRiderState(this);
    }

    // 设置当前状态
    public void setCurrentState(Player player, HenshinState state) {
        PlayerDataManager.get(player).setRiderState(this, state);
    }


    // 装备操作

    /**
     * 装备腰带
     * @param player 玩家实例
     */
    public void equipBelt(Player player) {
        player.setItemSlot(EquipmentSlot.LEGS, new ItemStack(getBeltItem()));
    }

    /**
     * 装备完整装备集
     * @param player 玩家实例
     */
    public void equipFullArmorSet(Player player) {
        for (int i = 0; i < SLOTS.length; i++) {
            player.setItemSlot(SLOTS[i], new ItemStack(armorSet[i]));
        }
    }


    // 核心getter
    public Item getBeltItem() {
        return armorSet[2]; // LEGS槽位对应腰带
    }

    //获取完整装备集
    public Item[] getFullArmorSet() {
        return armorSet;
    }

    public String getRiderName() {
        return riderName;
    }

    public Item getMainHenshinItem() {
        return mainHenshinItem;
    }

    public Item getOffhandHenshinItem() {
        return offhandHenshinItem;
    }


    // 以下方法在基础配置中提供默认实现
    // 在 FullRiderConfig 中可以覆盖这些方法

    public void clearGearData(Player player) {
        PlayerDataManager.get(player).clearGearData();
    }

    /**
     * 保存玩家装备数据
     * @param player 玩家实例
     * @return 包含装备数据的CompoundTag
     */
    public CompoundTag savePlayerGear(Player player) {
        return HenshinUtils.savePlayerGear(player);
    }

    /**
     * 加载玩家装备数据
     * @param player 玩家实例
     * @param gearData 包含装备数据的CompoundTag
     */
    public void setActiveGear(Player player, CompoundTag gearData) {
        PlayerDataManager.get(player).setActiveGear(gearData);
    }

    /**
     * 设置待机装备数据
     * @param player 玩家实例
     * @param gearData 包含装备数据的CompoundTag
     */
    public void setStandbyGear(Player player, CompoundTag gearData) {
        PlayerDataManager.get(player).setStandbyGear(gearData);
    }

    /**
     * 播放音效
     * @param player 玩家实例
     * @param type 音效类型
     */
    public void playSound(Player player, SoundType type) {
        // 基础配置没有音效实现
    }

    public void sendMessage(Player player, String message, ChatFormatting color) {
        HenshinUtils.sendMessage(player, message, color);
    }

    public Map<SoundType, SoundEvent> getSoundMap() {
        return Collections.emptyMap();
    }




}